home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python-1.4 / Lib / aifc.py < prev    next >
Text File  |  1998-06-24  |  30KB  |  1,016 lines

  1. # Stuff to parse AIFF-C and AIFF files.
  2. #
  3. # Unless explicitly stated otherwise, the description below is true
  4. # both for AIFF-C files and AIFF files.
  5. #
  6. # An AIFF-C file has the following structure.
  7. #
  8. #    +-----------------+
  9. #    | FORM            |
  10. #    +-----------------+
  11. #    | <size>          |
  12. #    +----+------------+
  13. #    |    | AIFC       |
  14. #    |    +------------+
  15. #    |    | <chunks>   |
  16. #    |    |    .       |
  17. #    |    |    .       |
  18. #    |    |    .       |
  19. #    +----+------------+
  20. #
  21. # An AIFF file has the string "AIFF" instead of "AIFC".
  22. #
  23. # A chunk consists of an identifier (4 bytes) followed by a size (4 bytes,
  24. # big endian order), followed by the data.  The size field does not include
  25. # the size of the 8 byte header.
  26. #
  27. # The following chunk types are recognized.
  28. #
  29. #    FVER
  30. #        <version number of AIFF-C defining document> (AIFF-C only).
  31. #    MARK
  32. #        <# of markers> (2 bytes)
  33. #        list of markers:
  34. #            <marker ID> (2 bytes, must be > 0)
  35. #            <position> (4 bytes)
  36. #            <marker name> ("pstring")
  37. #    COMM
  38. #        <# of channels> (2 bytes)
  39. #        <# of sound frames> (4 bytes)
  40. #        <size of the samples> (2 bytes)
  41. #        <sampling frequency> (10 bytes, IEEE 80-bit extended
  42. #            floating point)
  43. #        in AIFF-C files only:
  44. #        <compression type> (4 bytes)
  45. #        <human-readable version of compression type> ("pstring")
  46. #    SSND
  47. #        <offset> (4 bytes, not used by this program)
  48. #        <blocksize> (4 bytes, not used by this program)
  49. #        <sound data>
  50. #
  51. # A pstring consists of 1 byte length, a string of characters, and 0 or 1
  52. # byte pad to make the total length even.
  53. #
  54. # Usage.
  55. #
  56. # Reading AIFF files:
  57. #    f = aifc.open(file, 'r')
  58. # where file is either the name of a file or an open file pointer.
  59. # The open file pointer must have methods read(), seek(), and close().
  60. # In some types of audio files, if the setpos() method is not used,
  61. # the seek() method is not necessary.
  62. #
  63. # This returns an instance of a class with the following public methods:
  64. #    getnchannels()    -- returns number of audio channels (1 for
  65. #               mono, 2 for stereo)
  66. #    getsampwidth()    -- returns sample width in bytes
  67. #    getframerate()    -- returns sampling frequency
  68. #    getnframes()    -- returns number of audio frames
  69. #    getcomptype()    -- returns compression type ('NONE' for AIFF files)
  70. #    getcompname()    -- returns human-readable version of
  71. #               compression type ('not compressed' for AIFF files)
  72. #    getparams()    -- returns a tuple consisting of all of the
  73. #               above in the above order
  74. #    getmarkers()    -- get the list of marks in the audio file or None
  75. #               if there are no marks
  76. #    getmark(id)    -- get mark with the specified id (raises an error
  77. #               if the mark does not exist)
  78. #    readframes(n)    -- returns at most n frames of audio
  79. #    rewind()    -- rewind to the beginning of the audio stream
  80. #    setpos(pos)    -- seek to the specified position
  81. #    tell()        -- return the current position
  82. #    close()        -- close the instance (make it unusable)
  83. # The position returned by tell(), the position given to setpos() and
  84. # the position of marks are all compatible and have nothing to do with
  85. # the actual postion in the file.
  86. # The close() method is called automatically when the class instance
  87. # is destroyed.
  88. #
  89. # Writing AIFF files:
  90. #    f = aifc.open(file, 'w')
  91. # where file is either the name of a file or an open file pointer.
  92. # The open file pointer must have methods write(), tell(), seek(), and
  93. # close().
  94. #
  95. # This returns an instance of a class with the following public methods:
  96. #    aiff()        -- create an AIFF file (AIFF-C default)
  97. #    aifc()        -- create an AIFF-C file
  98. #    setnchannels(n)    -- set the number of channels
  99. #    setsampwidth(n)    -- set the sample width
  100. #    setframerate(n)    -- set the frame rate
  101. #    setnframes(n)    -- set the number of frames
  102. #    setcomptype(type, name)
  103. #            -- set the compression type and the
  104. #               human-readable compression type
  105. #    setparams(nchannels, sampwidth, framerate, nframes, comptype, compname)
  106. #            -- set all parameters at once
  107. #    setmark(id, pos, name)
  108. #            -- add specified mark to the list of marks
  109. #    tell()        -- return current position in output file (useful
  110. #               in combination with setmark())
  111. #    writeframesraw(data)
  112. #            -- write audio frames without pathing up the
  113. #               file header
  114. #    writeframes(data)
  115. #            -- write audio frames and patch up the file header
  116. #    close()        -- patch up the file header and close the
  117. #               output file
  118. # You should set the parameters before the first writeframesraw or
  119. # writeframes.  The total number of frames does not need to be set,
  120. # but when it is set to the correct value, the header does not have to
  121. # be patched up.
  122. # It is best to first set all parameters, perhaps possibly the
  123. # compression type, and then write audio frames using writeframesraw.
  124. # When all frames have been written, either call writeframes('') or
  125. # close() to patch up the sizes in the header.
  126. # Marks can be added anytime.  If there are any marks, ypu must call
  127. # close() after all frames have been written.
  128. # The close() method is called automatically when the class instance
  129. # is destroyed.
  130. #
  131. # When a file is opened with the extension '.aiff', an AIFF file is
  132. # written, otherwise an AIFF-C file is written.  This default can be
  133. # changed by calling aiff() or aifc() before the first writeframes or
  134. # writeframesraw.
  135.  
  136. import __builtin__
  137. try:
  138.     import CL
  139. except ImportError:
  140.     pass
  141.  
  142. Error = 'aifc.Error'
  143.  
  144. _AIFC_version = 0xA2805140        # Version 1 of AIFF-C
  145.  
  146. _skiplist = 'COMT', 'INST', 'MIDI', 'AESD', \
  147.       'APPL', 'NAME', 'AUTH', '(c) ', 'ANNO'
  148.  
  149. def _read_long(file):
  150.     x = 0L
  151.     for i in range(4):
  152.         byte = file.read(1)
  153.         if byte == '':
  154.             raise EOFError
  155.         x = x*256 + ord(byte)
  156.     if x >= 0x80000000L:
  157.         x = x - 0x100000000L
  158.     return int(x)
  159.  
  160. def _read_ulong(file):
  161.     x = 0L
  162.     for i in range(4):
  163.         byte = file.read(1)
  164.         if byte == '':
  165.             raise EOFError
  166.         x = x*256 + ord(byte)
  167.     return x
  168.  
  169. def _read_short(file):
  170.     x = 0
  171.     for i in range(2):
  172.         byte = file.read(1)
  173.         if byte == '':
  174.             raise EOFError
  175.         x = x*256 + ord(byte)
  176.     if x >= 0x8000:
  177.         x = x - 0x10000
  178.     return x
  179.  
  180. def _read_string(file):
  181.     length = ord(file.read(1))
  182.     if length == 0:
  183.         data = ''
  184.     else:
  185.         data = file.read(length)
  186.     if length & 1 == 0:
  187.         dummy = file.read(1)
  188.     return data
  189.  
  190. _HUGE_VAL = 1.79769313486231e+308 # See <limits.h>
  191.  
  192. def _read_float(f): # 10 bytes
  193.     import math
  194.     expon = _read_short(f) # 2 bytes
  195.     sign = 1
  196.     if expon < 0:
  197.         sign = -1
  198.         expon = expon + 0x8000
  199.     himant = _read_ulong(f) # 4 bytes
  200.     lomant = _read_ulong(f) # 4 bytes
  201.     if expon == himant == lomant == 0:
  202.         f = 0.0
  203.     elif expon == 0x7FFF:
  204.         f = _HUGE_VAL
  205.     else:
  206.         expon = expon - 16383
  207.         f = (himant * 0x100000000L + lomant) * pow(2.0, expon - 63)
  208.     return sign * f
  209.  
  210. def _write_short(f, x):
  211.     d, m = divmod(x, 256)
  212.     f.write(chr(d))
  213.     f.write(chr(m))
  214.  
  215. def _write_long(f, x):
  216.     if x < 0:
  217.         x = x + 0x100000000L
  218.     data = []
  219.     for i in range(4):
  220.         d, m = divmod(x, 256)
  221.         data.insert(0, m)
  222.         x = d
  223.     for i in range(4):
  224.         f.write(chr(int(data[i])))
  225.  
  226. def _write_string(f, s):
  227.     f.write(chr(len(s)))
  228.     f.write(s)
  229.     if len(s) & 1 == 0:
  230.         f.write(chr(0))
  231.  
  232. def _write_float(f, x):
  233.     import math
  234.     if x < 0:
  235.         sign = 0x8000
  236.         x = x * -1
  237.     else:
  238.         sign = 0
  239.     if x == 0:
  240.         expon = 0
  241.         himant = 0
  242.         lomant = 0
  243.     else:
  244.         fmant, expon = math.frexp(x)
  245.         if expon > 16384 or fmant >= 1:        # Infinity or NaN
  246.             expon = sign|0x7FFF
  247.             himant = 0
  248.             lomant = 0
  249.         else:                    # Finite
  250.             expon = expon + 16382
  251.             if expon < 0:            # denormalized
  252.                 fmant = math.ldexp(fmant, expon)
  253.                 expon = 0
  254.             expon = expon | sign
  255.             fmant = math.ldexp(fmant, 32)
  256.             fsmant = math.floor(fmant)
  257.             himant = long(fsmant)
  258.             fmant = math.ldexp(fmant - fsmant, 32)
  259.             fsmant = math.floor(fmant)
  260.             lomant = long(fsmant)
  261.     _write_short(f, expon)
  262.     _write_long(f, himant)
  263.     _write_long(f, lomant)
  264.  
  265. class Chunk:
  266.     def __init__(self, file):
  267.         self.file = file
  268.         self.chunkname = self.file.read(4)
  269.         if len(self.chunkname) < 4:
  270.             raise EOFError
  271.         self.chunksize = _read_long(self.file)
  272.         self.size_read = 0
  273.         self.offset = self.file.tell()
  274.  
  275.     def rewind(self):
  276.         self.file.seek(self.offset, 0)
  277.         self.size_read = 0
  278.  
  279.     def setpos(self, pos):
  280.         if pos < 0 or pos > self.chunksize:
  281.             raise RuntimeError
  282.         self.file.seek(self.offset + pos, 0)
  283.         self.size_read = pos
  284.         
  285.     def read(self, length):
  286.         if self.size_read >= self.chunksize:
  287.             return ''
  288.         if length > self.chunksize - self.size_read:
  289.              length = self.chunksize - self.size_read
  290.         data = self.file.read(length)
  291.         self.size_read = self.size_read + len(data)
  292.         return data
  293.  
  294.     def skip(self):
  295.         try:
  296.             self.file.seek(self.chunksize - self.size_read, 1)
  297.         except RuntimeError:
  298.             while self.size_read < self.chunksize:
  299.                 dummy = self.read(8192)
  300.                 if not dummy:
  301.                     raise EOFError
  302.         if self.chunksize & 1:
  303.             dummy = self.read(1)
  304.  
  305. class Aifc_read:
  306.     # Variables used in this class:
  307.     #
  308.     # These variables are available to the user though appropriate
  309.     # methods of this class:
  310.     # _file -- the open file with methods read(), close(), and seek()
  311.     #        set through the __init__() method
  312.     # _nchannels -- the number of audio channels
  313.     #        available through the getnchannels() method
  314.     # _nframes -- the number of audio frames
  315.     #        available through the getnframes() method
  316.     # _sampwidth -- the number of bytes per audio sample
  317.     #        available through the getsampwidth() method
  318.     # _framerate -- the sampling frequency
  319.     #        available through the getframerate() method
  320.     # _comptype -- the AIFF-C compression type ('NONE' if AIFF)
  321.     #        available through the getcomptype() method
  322.     # _compname -- the human-readable AIFF-C compression type
  323.     #        available through the getcomptype() method
  324.     # _markers -- the marks in the audio file
  325.     #        available through the getmarkers() and getmark()
  326.     #        methods
  327.     # _soundpos -- the position in the audio stream
  328.     #        available through the tell() method, set through the
  329.     #        setpos() method
  330.     #
  331.     # These variables are used internally only:
  332.     # _version -- the AIFF-C version number
  333.     # _decomp -- the decompressor from builtin module cl
  334.     # _comm_chunk_read -- 1 iff the COMM chunk has been read
  335.     # _aifc -- 1 iff reading an AIFF-C file
  336.     # _ssnd_seek_needed -- 1 iff positioned correctly in audio
  337.     #        file for readframes()
  338.     # _ssnd_chunk -- instantiation of a chunk class for the SSND chunk
  339.     # _framesize -- size of one frame in the file
  340.  
  341. ##     if 0: access _file, _nchannels, _nframes, _sampwidth, _framerate, \
  342. ##           _comptype, _compname, _markers, _soundpos, _version, \
  343. ##           _decomp, _comm_chunk_read, __aifc, _ssnd_seek_needed, \
  344. ##           _ssnd_chunk, _framesize: private
  345.  
  346.     def initfp(self, file):
  347.         self._file = file
  348.         self._version = 0
  349.         self._decomp = None
  350.         self._convert = None
  351.         self._markers = []
  352.         self._soundpos = 0
  353.         form = self._file.read(4)
  354.         if form != 'FORM':
  355.             raise Error, 'file does not start with FORM id'
  356.         formlength = _read_long(self._file)
  357.         if formlength <= 0:
  358.             raise Error, 'invalid FORM chunk data size'
  359.         formdata = self._file.read(4)
  360.         formlength = formlength - 4
  361.         if formdata == 'AIFF':
  362.             self._aifc = 0
  363.         elif formdata == 'AIFC':
  364.             self._aifc = 1
  365.         else:
  366.             raise Error, 'not an AIFF or AIFF-C file'
  367.         self._comm_chunk_read = 0
  368.         while formlength > 0:
  369.             self._ssnd_seek_needed = 1
  370.             #DEBUG: SGI's soundfiler has a bug.  There should
  371.             # be no need to check for EOF here.
  372.             try:
  373.                 chunk = Chunk(self._file)
  374.             except EOFError:
  375.                 if formlength == 8:
  376.                     print 'Warning: FORM chunk size too large'
  377.                     formlength = 0
  378.                     break
  379.                 raise EOFError # different error, raise exception
  380.             if chunk.chunkname == 'COMM':
  381.                 self._read_comm_chunk(chunk)
  382.                 self._comm_chunk_read = 1
  383.             elif chunk.chunkname == 'SSND':
  384.                 self._ssnd_chunk = chunk
  385.                 dummy = chunk.read(8)
  386.                 self._ssnd_seek_needed = 0
  387.             elif chunk.chunkname == 'FVER':
  388.                 self._version = _read_long(chunk)
  389.             elif chunk.chunkname == 'MARK':
  390.                 self._readmark(chunk)
  391.             elif chunk.chunkname in _skiplist:
  392.                 pass
  393.             else:
  394.                 raise Error, 'unrecognized chunk type '+chunk.chunkname
  395.             formlength = formlength - 8 - chunk.chunksize
  396.             if chunk.chunksize & 1:
  397.                 formlength = formlength - 1
  398.             if formlength > 0:
  399.                 chunk.skip()
  400.         if not self._comm_chunk_read or not self._ssnd_chunk:
  401.             raise Error, 'COMM chunk and/or SSND chunk missing'
  402.         if self._aifc and self._decomp:
  403.             params = [CL.ORIGINAL_FORMAT, 0,
  404.                   CL.BITS_PER_COMPONENT, self._sampwidth * 8,
  405.                   CL.FRAME_RATE, self._framerate]
  406.             if self._nchannels == 1:
  407.                 params[1] = CL.MONO
  408.             elif self._nchannels == 2:
  409.                 params[1] = CL.STEREO_INTERLEAVED
  410.             else:
  411.                 raise Error, 'cannot compress more than 2 channels'
  412.             self._decomp.SetParams(params)
  413.  
  414.     def __init__(self, f):
  415.         if type(f) == type(''):
  416.             f = __builtin__.open(f, 'rb')
  417.         # else, assume it is an open file object already
  418.         self.initfp(f)
  419.  
  420.     def __del__(self):
  421.         if self._file:
  422.             self.close()
  423.  
  424.     #
  425.     # User visible methods.
  426.     #
  427.     def getfp(self):
  428.         return self._file
  429.  
  430.     def rewind(self):
  431.         self._ssnd_seek_needed = 1
  432.         self._soundpos = 0
  433.  
  434.     def close(self):
  435.         if self._decomp:
  436.             self._decomp.CloseDecompressor()
  437.             self._decomp = None
  438.         self._file = None
  439.  
  440.     def tell(self):
  441.         return self._soundpos
  442.  
  443.     def getnchannels(self):
  444.         return self._nchannels
  445.  
  446.     def getnframes(self):
  447.         return self._nframes
  448.  
  449.     def getsampwidth(self):
  450.         return self._sampwidth
  451.  
  452.     def getframerate(self):
  453.         return self._framerate
  454.  
  455.     def getcomptype(self):
  456.         return self._comptype
  457.  
  458.     def getcompname(self):
  459.         return self._compname
  460.  
  461. ##    def getversion(self):
  462. ##        return self._version
  463.  
  464.     def getparams(self):
  465.         return self.getnchannels(), self.getsampwidth(), \
  466.               self.getframerate(), self.getnframes(), \
  467.               self.getcomptype(), self.getcompname()
  468.  
  469.     def getmarkers(self):
  470.         if len(self._markers) == 0:
  471.             return None
  472.         return self._markers
  473.  
  474.     def getmark(self, id):
  475.         for marker in self._markers:
  476.             if id == marker[0]:
  477.                 return marker
  478.         raise Error, 'marker ' + `id` + ' does not exist'
  479.  
  480.     def setpos(self, pos):
  481.         if pos < 0 or pos > self._nframes:
  482.             raise Error, 'position not in range'
  483.         self._soundpos = pos
  484.         self._ssnd_seek_needed = 1
  485.  
  486.     def readframes(self, nframes):
  487.         if self._ssnd_seek_needed:
  488.             self._ssnd_chunk.rewind()
  489.             dummy = self._ssnd_chunk.read(8)
  490.             pos = self._soundpos * self._framesize
  491.             if pos:
  492.                 self._ssnd_chunk.setpos(pos + 8)
  493.             self._ssnd_seek_needed = 0
  494.         if nframes == 0:
  495.             return ''
  496.         data = self._ssnd_chunk.read(nframes * self._framesize)
  497.         if self._convert and data:
  498.             data = self._convert(data)
  499.         self._soundpos = self._soundpos + len(data) / (self._nchannels * self._sampwidth)
  500.         return data
  501.  
  502.     #
  503.     # Internal methods.
  504.     #
  505. ##     if 0: access *: private
  506.  
  507.     def _decomp_data(self, data):
  508.         dummy = self._decomp.SetParam(CL.FRAME_BUFFER_SIZE,
  509.                           len(data) * 2)
  510.         return self._decomp.Decompress(len(data) / self._nchannels,
  511.                            data)
  512.  
  513.     def _ulaw2lin(self, data):
  514.         import audioop
  515.         return audioop.ulaw2lin(data, 2)
  516.  
  517.     def _adpcm2lin(self, data):
  518.         import audioop
  519.         if not hasattr(self, '_adpcmstate'):
  520.             # first time
  521.             self._adpcmstate = None
  522.         data, self._adpcmstate = audioop.adpcm2lin(data, 2,
  523.                                self._adpcmstate)
  524.         return data
  525.  
  526.     def _read_comm_chunk(self, chunk):
  527.         self._nchannels = _read_short(chunk)
  528.         self._nframes = _read_long(chunk)
  529.         self._sampwidth = (_read_short(chunk) + 7) / 8
  530.         self._framerate = int(_read_float(chunk))
  531.         self._framesize = self._nchannels * self._sampwidth
  532.         if self._aifc:
  533.             #DEBUG: SGI's soundeditor produces a bad size :-(
  534.             kludge = 0
  535.             if chunk.chunksize == 18:
  536.                 kludge = 1
  537.                 print 'Warning: bad COMM chunk size'
  538.                 chunk.chunksize = 23
  539.             #DEBUG end
  540.             self._comptype = chunk.read(4)
  541.             #DEBUG start
  542.             if kludge:
  543.                 length = ord(chunk.file.read(1))
  544.                 if length & 1 == 0:
  545.                     length = length + 1
  546.                 chunk.chunksize = chunk.chunksize + length
  547.                 chunk.file.seek(-1, 1)
  548.             #DEBUG end
  549.             self._compname = _read_string(chunk)
  550.             if self._comptype != 'NONE':
  551.                 if self._comptype == 'G722':
  552.                     try:
  553.                         import audioop
  554.                     except ImportError:
  555.                         pass
  556.                     else:
  557.                         self._convert = self._adpcm2lin
  558.                         self._framesize = self._framesize / 4
  559.                         return
  560.                 # for ULAW and ALAW try Compression Library
  561.                 try:
  562.                     import cl, CL
  563.                 except ImportError:
  564.                     if self._comptype == 'ULAW':
  565.                         try:
  566.                             import audioop
  567.                             self._convert = self._ulaw2lin
  568.                             self._framesize = self._framesize / 2
  569.                             return
  570.                         except ImportError:
  571.                             pass
  572.                     raise Error, 'cannot read compressed AIFF-C files'
  573.                 if self._comptype == 'ULAW':
  574.                     scheme = CL.G711_ULAW
  575.                     self._framesize = self._framesize / 2
  576.                 elif self._comptype == 'ALAW':
  577.                     scheme = CL.G711_ALAW
  578.                     self._framesize = self._framesize / 2
  579.                 else:
  580.                     raise Error, 'unsupported compression type'
  581.                 self._decomp = cl.OpenDecompressor(scheme)
  582.                 self._convert = self._decomp_data
  583.         else:
  584.             self._comptype = 'NONE'
  585.             self._compname = 'not compressed'
  586.  
  587.     def _readmark(self, chunk):
  588.         nmarkers = _read_short(chunk)
  589.         # Some files appear to contain invalid counts.
  590.         # Cope with this by testing for EOF.
  591.         try:
  592.             for i in range(nmarkers):
  593.                 id = _read_short(chunk)
  594.                 pos = _read_long(chunk)
  595.                 name = _read_string(chunk)
  596.                 if pos or name:
  597.                     # some files appear to have
  598.                     # dummy markers consisting of
  599.                     # a position 0 and name ''
  600.                     self._markers.append((id, pos, name))
  601.         except EOFError:
  602.             print 'Warning: MARK chunk contains only',
  603.             print len(self._markers),
  604.             if len(self._markers) == 1: print 'marker',
  605.             else: print 'markers',
  606.             print 'instead of', nmarkers
  607.  
  608. class Aifc_write:
  609.     # Variables used in this class:
  610.     #
  611.     # These variables are user settable through appropriate methods
  612.     # of this class:
  613.     # _file -- the open file with methods write(), close(), tell(), seek()
  614.     #        set through the __init__() method
  615.     # _comptype -- the AIFF-C compression type ('NONE' in AIFF)
  616.     #        set through the setcomptype() or setparams() method
  617.     # _compname -- the human-readable AIFF-C compression type
  618.     #        set through the setcomptype() or setparams() method
  619.     # _nchannels -- the number of audio channels
  620.     #        set through the setnchannels() or setparams() method
  621.     # _sampwidth -- the number of bytes per audio sample
  622.     #        set through the setsampwidth() or setparams() method
  623.     # _framerate -- the sampling frequency
  624.     #        set through the setframerate() or setparams() method
  625.     # _nframes -- the number of audio frames written to the header
  626.     #        set through the setnframes() or setparams() method
  627.     # _aifc -- whether we're writing an AIFF-C file or an AIFF file
  628.     #        set through the aifc() method, reset through the
  629.     #        aiff() method
  630.     #
  631.     # These variables are used internally only:
  632.     # _version -- the AIFF-C version number
  633.     # _comp -- the compressor from builtin module cl
  634.     # _nframeswritten -- the number of audio frames actually written
  635.     # _datalength -- the size of the audio samples written to the header
  636.     # _datawritten -- the size of the audio samples actually written
  637.  
  638. ##     if 0: access _file, _comptype, _compname, _nchannels, _sampwidth, \
  639. ##           _framerate, _nframes, _aifc, _version, _comp, \
  640. ##           _nframeswritten, _datalength, _datawritten: private
  641.  
  642.     def __init__(self, f):
  643.         if type(f) == type(''):
  644.             filename = f
  645.             f = __builtin__.open(f, 'wb')
  646.         else:
  647.             # else, assume it is an open file object already
  648.             filename = '???'
  649.         self.initfp(f)
  650.         if filename[-5:] == '.aiff':
  651.             self._aifc = 0
  652.         else:
  653.             self._aifc = 1
  654.  
  655.     def initfp(self, file):
  656.         self._file = file
  657.         self._version = _AIFC_version
  658.         self._comptype = 'NONE'
  659.         self._compname = 'not compressed'
  660.         self._comp = None
  661.         self._convert = None
  662.         self._nchannels = 0
  663.         self._sampwidth = 0
  664.         self._framerate = 0
  665.         self._nframes = 0
  666.         self._nframeswritten = 0
  667.         self._datawritten = 0
  668.         self._datalength = 0
  669.         self._markers = []
  670.         self._marklength = 0
  671.         self._aifc = 1        # AIFF-C is default
  672.  
  673.     def __del__(self):
  674.         if self._file:
  675.             self.close()
  676.  
  677.     #
  678.     # User visible methods.
  679.     #
  680.     def aiff(self):
  681.         if self._nframeswritten:
  682.             raise Error, 'cannot change parameters after starting to write'
  683.         self._aifc = 0
  684.  
  685.     def aifc(self):
  686.         if self._nframeswritten:
  687.             raise Error, 'cannot change parameters after starting to write'
  688.         self._aifc = 1
  689.  
  690.     def setnchannels(self, nchannels):
  691.         if self._nframeswritten:
  692.             raise Error, 'cannot change parameters after starting to write'
  693.         if nchannels < 1:
  694.             raise Error, 'bad # of channels'
  695.         self._nchannels = nchannels
  696.  
  697.     def getnchannels(self):
  698.         if not self._nchannels:
  699.             raise Error, 'number of channels not set'
  700.         return self._nchannels
  701.  
  702.     def setsampwidth(self, sampwidth):
  703.         if self._nframeswritten:
  704.             raise Error, 'cannot change parameters after starting to write'
  705.         if sampwidth < 1 or sampwidth > 4:
  706.             raise Error, 'bad sample width'
  707.         self._sampwidth = sampwidth
  708.  
  709.     def getsampwidth(self):
  710.         if not self._sampwidth:
  711.             raise Error, 'sample width not set'
  712.         return self._sampwidth
  713.  
  714.     def setframerate(self, framerate):
  715.         if self._nframeswritten:
  716.             raise Error, 'cannot change parameters after starting to write'
  717.         if framerate <= 0:
  718.             raise Error, 'bad frame rate'
  719.         self._framerate = framerate
  720.  
  721.     def getframerate(self):
  722.         if not self._framerate:
  723.             raise Error, 'frame rate not set'
  724.         return self._framerate
  725.  
  726.     def setnframes(self, nframes):
  727.         if self._nframeswritten:
  728.             raise Error, 'cannot change parameters after starting to write'
  729.         self._nframes = nframes
  730.  
  731.     def getnframes(self):
  732.         return self._nframeswritten
  733.  
  734.     def setcomptype(self, comptype, compname):
  735.         if self._nframeswritten:
  736.             raise Error, 'cannot change parameters after starting to write'
  737.         if comptype not in ('NONE', 'ULAW', 'ALAW', 'G722'):
  738.             raise Error, 'unsupported compression type'
  739.         self._comptype = comptype
  740.         self._compname = compname
  741.  
  742.     def getcomptype(self):
  743.         return self._comptype
  744.  
  745.     def getcompname(self):
  746.         return self._compname
  747.  
  748. ##    def setversion(self, version):
  749. ##        if self._nframeswritten:
  750. ##            raise Error, 'cannot change parameters after starting to write'
  751. ##        self._version = version
  752.  
  753.     def setparams(self, (nchannels, sampwidth, framerate, nframes, comptype, compname)):
  754.         if self._nframeswritten:
  755.             raise Error, 'cannot change parameters after starting to write'
  756.         if comptype not in ('NONE', 'ULAW', 'ALAW', 'G722'):
  757.             raise Error, 'unsupported compression type'
  758.         self.setnchannels(nchannels)
  759.         self.setsampwidth(sampwidth)
  760.         self.setframerate(framerate)
  761.         self.setnframes(nframes)
  762.         self.setcomptype(comptype, compname)
  763.  
  764.     def getparams(self):
  765.         if not self._nchannels or not self._sampwidth or not self._framerate:
  766.             raise Error, 'not all parameters set'
  767.         return self._nchannels, self._sampwidth, self._framerate, \
  768.               self._nframes, self._comptype, self._compname
  769.  
  770.     def setmark(self, id, pos, name):
  771.         if id <= 0:
  772.             raise Error, 'marker ID must be > 0'
  773.         if pos < 0:
  774.             raise Error, 'marker position must be >= 0'
  775.         if type(name) != type(''):
  776.             raise Error, 'marker name must be a string'
  777.         for i in range(len(self._markers)):
  778.             if id == self._markers[i][0]:
  779.                 self._markers[i] = id, pos, name
  780.                 return
  781.         self._markers.append((id, pos, name))
  782.  
  783.     def getmark(self, id):
  784.         for marker in self._markers:
  785.             if id == marker[0]:
  786.                 return marker
  787.         raise Error, 'marker ' + `id` + ' does not exist'
  788.  
  789.     def getmarkers(self):
  790.         if len(self._markers) == 0:
  791.             return None
  792.         return self._markers
  793.                 
  794.     def tell(self):
  795.         return self._nframeswritten
  796.  
  797.     def writeframesraw(self, data):
  798.         self._ensure_header_written(len(data))
  799.         nframes = len(data) / (self._sampwidth * self._nchannels)
  800.         if self._convert:
  801.             data = self._convert(data)
  802.         self._file.write(data)
  803.         self._nframeswritten = self._nframeswritten + nframes
  804.         self._datawritten = self._datawritten + len(data)
  805.  
  806.     def writeframes(self, data):
  807.         self.writeframesraw(data)
  808.         if self._nframeswritten != self._nframes or \
  809.               self._datalength != self._datawritten:
  810.             self._patchheader()
  811.  
  812.     def close(self):
  813.         self._ensure_header_written(0)
  814.         if self._datawritten & 1:
  815.             # quick pad to even size
  816.             self._file.write(chr(0))
  817.             self._datawritten = self._datawritten + 1
  818.         self._writemarkers()
  819.         if self._nframeswritten != self._nframes or \
  820.               self._datalength != self._datawritten or \
  821.               self._marklength:
  822.             self._patchheader()
  823.         if self._comp:
  824.             self._comp.CloseCompressor()
  825.             self._comp = None
  826.         self._file.flush()
  827.         self._file = None
  828.  
  829.     #
  830.     # Internal methods.
  831.     #
  832. ##     if 0: access *: private
  833.  
  834.     def _comp_data(self, data):
  835.         dum = self._comp.SetParam(CL.FRAME_BUFFER_SIZE, len(data))
  836.         dum = self._comp.SetParam(CL.COMPRESSED_BUFFER_SIZE, len(data))
  837.         return self._comp.Compress(nframes, data)
  838.  
  839.     def _lin2ulaw(self, data):
  840.         import audioop
  841.         return audioop.lin2ulaw(data, 2)
  842.  
  843.     def _lin2adpcm(self, data):
  844.         import audioop
  845.         if not hasattr(self, '_adpcmstate'):
  846.             self._adpcmstate = None
  847.         data, self._adpcmstate = audioop.lin2adpcm(data, 2,
  848.                                self._adpcmstate)
  849.         return data
  850.  
  851.     def _ensure_header_written(self, datasize):
  852.         if not self._nframeswritten:
  853.             if self._comptype in ('ULAW', 'ALAW'):
  854.                 if not self._sampwidth:
  855.                     self._sampwidth = 2
  856.                 if self._sampwidth != 2:
  857.                     raise Error, 'sample width must be 2 when compressing with ULAW or ALAW'
  858.             if self._comptype == 'G722':
  859.                 if not self._sampwidth:
  860.                     self._sampwidth = 2
  861.                 if self._sampwidth != 2:
  862.                     raise Error, 'sample width must be 2 when compressing with G7.22 (ADPCM)'
  863.             if not self._nchannels:
  864.                 raise Error, '# channels not specified'
  865.             if not self._sampwidth:
  866.                 raise Error, 'sample width not specified'
  867.             if not self._framerate:
  868.                 raise Error, 'sampling rate not specified'
  869.             self._write_header(datasize)
  870.  
  871.     def _init_compression(self):
  872.         if self._comptype == 'G722':
  873.             import audioop
  874.             self._convert = self._lin2adpcm
  875.             return
  876.         try:
  877.             import cl, CL
  878.         except ImportError:
  879.             if self._comptype == 'ULAW':
  880.                 try:
  881.                     import audioop
  882.                     self._convert = self._lin2ulaw
  883.                     return
  884.                 except ImportError:
  885.                     pass
  886.             raise Error, 'cannot write compressed AIFF-C files'
  887.         if self._comptype == 'ULAW':
  888.             scheme = CL.G711_ULAW
  889.         elif self._comptype == 'ALAW':
  890.             scheme = CL.G711_ALAW
  891.         else:
  892.             raise Error, 'unsupported compression type'
  893.         self._comp = cl.OpenCompressor(scheme)
  894.         params = [CL.ORIGINAL_FORMAT, 0,
  895.               CL.BITS_PER_COMPONENT, self._sampwidth * 8,
  896.               CL.FRAME_RATE, self._framerate,
  897.               CL.FRAME_BUFFER_SIZE, 100,
  898.               CL.COMPRESSED_BUFFER_SIZE, 100]
  899.         if self._nchannels == 1:
  900.             params[1] = CL.MONO
  901.         elif self._nchannels == 2:
  902.             params[1] = CL.STEREO_INTERLEAVED
  903.         else:
  904.             raise Error, 'cannot compress more than 2 channels'
  905.         self._comp.SetParams(params)
  906.         # the compressor produces a header which we ignore
  907.         dummy = self._comp.Compress(0, '')
  908.         self._convert = self._comp_data
  909.  
  910.     def _write_header(self, initlength):
  911.         if self._aifc and self._comptype != 'NONE':
  912.             self._init_compression()
  913.         self._file.write('FORM')
  914.         if not self._nframes:
  915.             self._nframes = initlength / (self._nchannels * self._sampwidth)
  916.         self._datalength = self._nframes * self._nchannels * self._sampwidth
  917.         if self._datalength & 1:
  918.             self._datalength = self._datalength + 1
  919.         if self._aifc:
  920.             if self._comptype in ('ULAW', 'ALAW'):
  921.                 self._datalength = self._datalength / 2
  922.                 if self._datalength & 1:
  923.                     self._datalength = self._datalength + 1
  924.             elif self._comptype == 'G722':
  925.                 self._datalength = (self._datalength + 3) / 4
  926.                 if self._datalength & 1:
  927.                     self._datalength = self._datalength + 1
  928.         self._form_length_pos = self._file.tell()
  929.         commlength = self._write_form_length(self._datalength)
  930.         if self._aifc:
  931.             self._file.write('AIFC')
  932.             self._file.write('FVER')
  933.             _write_long(self._file, 4)
  934.             _write_long(self._file, self._version)
  935.         else:
  936.             self._file.write('AIFF')
  937.         self._file.write('COMM')
  938.         _write_long(self._file, commlength)
  939.         _write_short(self._file, self._nchannels)
  940.         self._nframes_pos = self._file.tell()
  941.         _write_long(self._file, self._nframes)
  942.         _write_short(self._file, self._sampwidth * 8)
  943.         _write_float(self._file, self._framerate)
  944.         if self._aifc:
  945.             self._file.write(self._comptype)
  946.             _write_string(self._file, self._compname)
  947.         self._file.write('SSND')
  948.         self._ssnd_length_pos = self._file.tell()
  949.         _write_long(self._file, self._datalength + 8)
  950.         _write_long(self._file, 0)
  951.         _write_long(self._file, 0)
  952.  
  953.     def _write_form_length(self, datalength):
  954.         if self._aifc:
  955.             commlength = 18 + 5 + len(self._compname)
  956.             if commlength & 1:
  957.                 commlength = commlength + 1
  958.             verslength = 12
  959.         else:
  960.             commlength = 18
  961.             verslength = 0
  962.         _write_long(self._file, 4 + verslength + self._marklength + \
  963.                     8 + commlength + 16 + datalength)
  964.         return commlength
  965.  
  966.     def _patchheader(self):
  967.         curpos = self._file.tell()
  968.         if self._datawritten & 1:
  969.             datalength = self._datawritten + 1
  970.             self._file.write(chr(0))
  971.         else:
  972.             datalength = self._datawritten
  973.         if datalength == self._datalength and \
  974.               self._nframes == self._nframeswritten and \
  975.               self._marklength == 0:
  976.             self._file.seek(curpos, 0)
  977.             return
  978.         self._file.seek(self._form_length_pos, 0)
  979.         dummy = self._write_form_length(datalength)
  980.         self._file.seek(self._nframes_pos, 0)
  981.         _write_long(self._file, self._nframeswritten)
  982.         self._file.seek(self._ssnd_length_pos, 0)
  983.         _write_long(self._file, datalength + 8)
  984.         self._file.seek(curpos, 0)
  985.         self._nframes = self._nframeswritten
  986.         self._datalength = datalength
  987.  
  988.     def _writemarkers(self):
  989.         if len(self._markers) == 0:
  990.             return
  991.         self._file.write('MARK')
  992.         length = 2
  993.         for marker in self._markers:
  994.             id, pos, name = marker
  995.             length = length + len(name) + 1 + 6
  996.             if len(name) & 1 == 0:
  997.                 length = length + 1
  998.         _write_long(self._file, length)
  999.         self._marklength = length + 8
  1000.         _write_short(self._file, len(self._markers))
  1001.         for marker in self._markers:
  1002.             id, pos, name = marker
  1003.             _write_short(self._file, id)
  1004.             _write_long(self._file, pos)
  1005.             _write_string(self._file, name)
  1006.  
  1007. def open(f, mode):
  1008.     if mode == 'r':
  1009.         return Aifc_read(f)
  1010.     elif mode == 'w':
  1011.         return Aifc_write(f)
  1012.     else:
  1013.         raise Error, "mode must be 'r' or 'w'"
  1014.  
  1015. openfp = open # B/W compatibility
  1016.